[interpreter] Validate offsets against the selected memory - #2233
Open
sanguineman wants to merge 1 commit into
Open
[interpreter] Validate offsets against the selected memory#2233sanguineman wants to merge 1 commit into
sanguineman wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
WebAssembly load instructions consume a base address from the operand stack, add the static offset encoded in the instruction, and read a value from the resulting address in the selected linear memory.
The selected memory's address type determines both the type of the base address and the valid range of the static offset. A memory32 instruction consumes an
i32base address and requires its offset to be less than2^32. A memory64 instruction consumes ani64base address and permits offsets in the wideru64range.The validation specification written in SpecTec checks these conditions using the address type of the selected memory.
However, the OCaml reference interpreter uses two different memories while validating a memory instruction. The instruction validator obtains the stack address type from the selected memory
x, butcheck_memopobtains the address type used for offset validation from memory 0.As a result, offset validation can use the wrong address type when the selected memory and memory 0 have different address types.
For example, consider a module where memory 0 is a memory64 and memory 1 is a memory32:
A load from memory 1 with an offset of
4294967296must be rejected because the selected memory is a memory32 and the offset is equal to2^32.The reference interpreter instead checks the offset against the address type of memory 0. Because memory 0 is a memory64, it skips the memory32 offset bound and accepts the invalid instruction.
The declarative SpecTec validation rule does not have this mismatch. It obtains the address type
atfromC.MEMS[x]and passes the same address type toMemarg_ok:Fix
This PR passes the selected memory index
xtocheck_memopand uses that memory to validate the offset. The stack address type and the offset bound are therefore derived from the same selected memory.Because
check_memopis shared by numeric and vector load/store instructions, the correction applies consistently toLoad,Store,VecLoad,VecStore,VecLoadLane, andVecStoreLane.A regression test declares memory 0 as memory64 and memory 1 as memory32, then verifies that a load from memory 1 with an offset of
2^32is rejected withoffset out of range.Testing
make -C interpreter test/memory64/load64make -C interpreter unittest